<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--任务管理数据-->
      <el-col>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">

          <el-form-item label="时间" prop="planComplete" label-width="120px">
            <el-date-picker
              v-model="planComplete"
              clearable
              type="daterange"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>

           <el-form-item label="监督员" prop="supervisor" label-width="120px">
            <el-input
              v-model="queryParams.supervisor"
              placeholder="请输入监督员"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

       <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              size="mini"
              type="primary"
              @click="handleTest('RANDOM')"
            >随机生成</el-button>
            <el-button
              size="mini"
              type="primary"
              @click="handleTest('DISPOSITION')"  
            >配置比例</el-button>
          </el-col>
    </el-row>

        <el-table v-loading="loading" :data="infoList">
           <el-table-column label="序号" align="center" type="index" />
          <el-table-column label="被监督单位" align="center" prop="enterpriseName" />
          <el-table-column label="抽查专业" align="center" prop="checkProfessionName" />
          <el-table-column label="镇/街道" align="center" prop="town" />
          <el-table-column label="经营地址" align="center" prop="addr" />
          <el-table-column label="监督机构" align="center" prop="superviseOrg" />
          <el-table-column label="监管员" align="center" prop="supervisor"/>
          <el-table-column label="计划完成时间" align="center" prop="planCompletionTime"/>
          <el-table-column label="执行状态" align="center" prop="implementStatus" >
            <template slot-scope="scope">
            <span style="margin-left: 10px">{{
              scope.row.status == "1"
                ? "执行中"
                : scope.row.status == "2"
                ? "已完成"
                : "未执行"
            }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width">
          <template slot-scope="scope">
                <el-button
                    size="mini"
                    type="text"
                    @click="handleInfo(scope.row)"
                >查看详情</el-button>
        </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getSpotCheckFn"
        />
      </el-col>
    </el-row>

  
    <el-dialog title="随机生成抽查任务" :visible.sync="open" width="75%" append-to-body>
        <el-form :model="from" ref="formData" size="small" :inline="true">
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划完成时间" prop="planCompletionTime">
               <el-date-picker
                v-model="from.planCompletionTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="date"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        </el-form>

        <el-table :data="spotCheck.taskList" border >
          <el-table-column label="名称" align="center" prop="enterpriseName" />
          <el-table-column label="统一信用代码/登记号" align="center"  prop="creditCode"/>
          <el-table-column label="检查任务" align="center" prop="checkProfessionName"/>
         <el-table-column label="类别" align="center" prop="categoryListName"/>
          <el-table-column label="所在镇/街道" align="center" prop="town" />
          <el-table-column label="详细地址" align="center" prop="addr"/>
          <el-table-column label="联系电话" align="center" prop="" />
          <el-table-column label="监督员" align="center" prop="supervisor" />
        </el-table>
        <!-- <pagination
          v-show="enterpriseInfoTotal>0"
          :total="enterpriseInfoTotal"
          :page.sync="enterpriseInfoQueryParams.pageNum"
          :limit.sync="enterpriseInfoQueryParams.pageSize"
          @pagination="getList"
        />
        <el-table></el-table> -->
      <div slot="footer" class="dialog-footer">
        <!-- <el-button @click="cancel">取 消</el-button> -->
        <el-button type="primary" @click="submitForm('RANDOM')">保 存</el-button>
      </div>
    </el-dialog>

    <el-dialog title="配置生成比例" :visible.sync="scaleOpen" width="460px" append-to-body>
        <div>
               <div class="fromList">
                     <div class="lable">职业卫生</div>
                     <div> <el-input v-model="dispositionData" placeholder="请输入生成数量"></el-input></div>
               </div>
        </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('DISPOSITION')">确 认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { getSpotCheck,
           getRandomGenPreView,
           getSaveRandomGen,
           getDisposition,
           getSaveScale
           } from "@/api/zhangping/occupational";
  export default {
    data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          infoList: [],
          spotCheck:{},
          dispositionData:'',
          planComplete:'',
          // 查询参数
          queryParams: {  
              planCompletionStartTime:'', // 开始时间
              planCompletionEndTime:'',  // 结束时间
              supervisor:'', // 监管人员  
              pageNum: 1,
              pageSize: 10,
          },
          from:{
             planCompletionTime:''
          },
          open:false,
          scaleOpen:false,
      };
    },

    created() {
       this.loading = false
       this.getSpotCheckFn()
    },
    methods: {
      // 获取列表 
     getSpotCheckFn(){
          if(this.planComplete !=null && this.planComplete != undefined){
              if(this.planComplete.length > 0){
                    this.queryParams.planCompletionStartTime = this.planComplete[0]
                    this.queryParams.planCompletionEndTime = this.planComplete[1]
              }    
          }else{
               this.queryParams.planCompletionStartTime = ''
               this.queryParams.planCompletionEndTime = ''
          }       
         getSpotCheck(this.queryParams).then(res=>{
                if(res.code == 200){
                     this.infoList = res.rows
                     this.total = res.total
                }else{
                     this.$message.error(res.msg);  
                }    
         })
    },
    getRandomGenPreViewFn(){
          getRandomGenPreView().then(res=>{
                if(res.code == 200){
                      this.spotCheck = res.data
                }else{
                      this.$message.error(res.msg);  
                }    
          })
    },
    getDispositionFn(){
         getDisposition().then(res=>{
                if(res.code == 200){
                      this.dispositionData = res.data
                }else{
                      this.$message.error(res.msg);  
                }    
          })
    },
   getSaveRandomGenFn(){
              let data ={
                     cacheKey:this.spotCheck.cacheKey,
                     planCompletionTime:this.from.planCompletionTime
                }
                getSaveRandomGen(data).then(res=>{
                          if(res.code == 200){
                                this.$message.success(res.msg);
                                this.open = false
                                this.getSpotCheckFn()
                                this.reset()
                          }else{
                                this.$message.error(res.msg);  
                          }    
                    })
      },
    getSaveScaleFn(){
        let data ={
                  percent:this.dispositionData,
                }
         getSaveScale(data).then(res=>{
                if(res.code == 200){
                      this.$message.success(res.msg);
                      this.scaleOpen = false
                      this.getSpotCheckFn()
                      this.reset()
                }else{
                      this.$message.error(res.msg);  
                }    
          })
                
    },
    submitForm(type){
          if(type === 'RANDOM'){
            this.getSaveRandomGenFn()
          }else{
             this.getSaveScaleFn()
          }
         
    },
    // 表单重置
    reset() {
      this.queryParams = {
          planCompletionStartTime:'', // 开始时间
          planCompletionEndTime:'',  // 结束时间
          supervisor:'',   // 监督人员
          pageNum: 1,
          pageSize: 10,
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getSpotCheckFn()
    },
    handleTest(type){  
          if(type === 'RANDOM'){
              this.open = true
              this.getRandomGenPreViewFn()    
          }else{
              this.scaleOpen = true
              this.getDispositionFn()
          }
    },
    handleInfo(){
        let type = "SPOTCHECK"
        this.$router.push(`/careerHygiene/taskInfo/inspectDetail?type=${type}`)  
    },
    cancel(){
         this.open = false
         this.scaleOpen = false
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.handleQuery();
    },
    }
  };
</script>

<style lang="scss" scoped>
      .display-fx-center{
             display: flex;
             align-items: center;
         }
     .fromList{
           padding-top:20px; 
          @extend .display-fx-center;
          .lable{
              font-size: 14px;
              color: #484848;
              width: 90px;
              flex-shrink: 0;
              text-align: right;
              margin-right:10px;
          }
     }
    ::v-deep{
        .el-dialog__header{
           font-size: 22px;
           color: #484848;
           font-weight: bold;
        }
        .el-dialog__body{
            padding: 0 20px 30px 20px;
        }   
        .fromList{    
            .el-input__inner{
                  width: 300px;
            }
        }
    }
</style>
